<?php

class PartnerController extends Zend_Controller_Action 
{
    protected $_model;
	
	function indexAction()
	{
		$model = $this->_getModel();
		$view_list = $this->_model;
		$rowset = $view_list->fetchAll();

		$rowCount = count($rowset);
		$this->view->view_list = $rowset;
		$this->view->rowCount  = $rowCount;
	}
	
	function addAction()
	{
		$form = $this->_getPartnerForm();
		
        $form->submit->setLabel('Add');
        $this->view->form = $form;

		$request = $this->getRequest();
		if ($this->_request->isPost()) {
			$formData = $this->_request->getPost();
            if ($form->isValid($formData)) {
            	
				$model = $this->_getModel();
				$add = $this->_model;
				
				$formData = $form->getValues();
                
				$row 			  	  = $add->createRow();
                $row->razao_social    = $form->getValue('razao_social');
                $row->cnpj            = $form->getValue('cnpj');
                $row->endereco        = $form->getValue('endereco');
                $row->complemento     = $form->getValue('complemento');
                $row->bairro          = $form->getValue('bairro');
                $row->cidade          = $form->getValue('cidade');
                $row->estado          = $form->getValue('estado');
                $row->ddd1            = $form->getValue('ddd1');
				$row->tel1            = $form->getValue('tel1');
				$row->ddd2            = $form->getValue('ddd2');
				$row->tel2            = $form->getValue('tel2');
				$row->contato         = $form->getValue('contato');
				$row->obs             = $form->getValue('obs');
				
                $row->save();
                
                return $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        }
	}

	function showAction()
    {
		$cod_conveniado = (int)$this->_request->getParam('cod_conveniado');
				
		$model = $this->_getModel();
		$show = $this->_model;
		
		$this->view->show = $show->fetchRow('cod_conveniado='.$cod_conveniado);
	}

	function editAction()
    {
        $form = $this->_getPartnerForm();
		
        $form->submit->setLabel('Save');
        $this->view->form = $form;
        
        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();
            if ($form->isValid($formData)) {
                
				$model = $this->_getModel();
				$edit = $this->_model;
                
				$cod_conveniado = (int)$form->getValue('cod_conveniado');
                $row 	        = $edit->fetchRow('cod_conveniado='.$cod_conveniado);

                $row->razao_social    = $form->getValue('razao_social');
                $row->cnpj            = $form->getValue('cnpj');
                $row->endereco        = $form->getValue('endereco');
                $row->complemento     = $form->getValue('complemento');
                $row->bairro          = $form->getValue('bairro');
                $row->cidade          = $form->getValue('cidade');
                $row->estado          = $form->getValue('estado');
                $row->ddd1            = $form->getValue('ddd1');
				$row->tel1            = $form->getValue('tel1');
				$row->ddd2            = $form->getValue('ddd2');
				$row->tel2            = $form->getValue('tel2');
				$row->contato         = $form->getValue('contato');
				$row->obs             = $form->getValue('obs');				
                $row->save();
                
                return $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        } else {
            $cod_conveniado = (int)$this->_request->getParam('cod_conveniado', 0);
            if ($cod_conveniado > 0) {
                
				$model = $this->_getModel();
				$edit = $this->_model;
				
                $edit = $edit->fetchRow('cod_conveniado='.$cod_conveniado);
                $form->populate($edit->toArray());
            }
        }
    }

	function deleteAction()
    {        
        if ($this->_request->isPost()) {
            $cod_conveniado = (int)$this->_request->getPost('cod_conveniado');
            $del = $this->_request->getPost('del');
            if ($del == 'Yes' && $cod_conveniado > 0) {
                
				$model = $this->_getModel();
				$remove = $this->_model;
				
                $where = 'cod_conveniado = ' . $cod_conveniado;
                $remove->delete($where);
            }
            return $this->_helper->redirector('index');
        } else {
            $cod_conveniado = (int)$this->_request->getParam('cod_conveniado');
            if ($cod_conveniado > 0) {
                
				$model = $this->_getModel();
				$remove = $this->_model;
				
                $this->view->remove = $remove->fetchRow('cod_conveniado='.$cod_conveniado);
            }
        }
    }

    protected function _getModel()
    {
        if (null === $this->_model) {
            require_once APPLICATION_PATH . '/models/Partner.php';
            $this->_model = new Model_Partner();
        }
        return $this->_model;
    }
	
	protected function _getPartnerForm()
    {
        require_once APPLICATION_PATH . '/forms/PartnerForm.php';
        $form = new Form_Partner();
        return $form;
    }

}

